package com.geo.smallcredit.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.geo.smallcredit.R;
import com.geo.smallcredit.MainApplication.MainApplication;
import com.geo.smallcredit.adapter.ViewPagerAdapter;
import com.geo.smallcredit.util.SharedPreferencesUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

public class WelcomActivity extends Activity implements OnPageChangeListener,
		OnClickListener {

	public static final String TAG = "WelcomActivity";

//	@ViewInject(R.id.image_welcome)
	private ImageView mImageView;
	private Context mContext;
	private String version;
//	private Button registerBtn, loginBtn;
	private ViewPager vp;
	private ViewPagerAdapter vpAdapter;
	private List<View> views;
	private boolean isExit;
	// 引导图片资源
	 private static final int[] pics = { R.drawable.lp1, R.drawable.lp2,
	 R.drawable.lp3 };

	private int currentIndex;
	private String userid,token;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MainApplication.getInstance().addActivity(WelcomActivity.this);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		MainApplication.getInstance().addActivity(WelcomActivity.this);

		 userid = SharedPreferencesUtils.getString(this,"userid", null);
		token=SharedPreferencesUtils.getString(WelcomActivity.this, "token", null);
//		if (!"".equals(userid)&&!"".equals(token)&&userid!=null&&token!=null) {
//			startActivity(new Intent(this, MainActivity.class));
//		} else {
			setContentView(R.layout.activity_welcom);
//			initView();
//			initClick();
			ViewUtils.inject(this);
			mContext = this;
			// 褰撳墠搴旂敤鐗堟湰鍙?
			version = getVersion();
			// Log.e(TAG, version);
		
		views = new ArrayList<View>();
		LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);

		for (int i = 0; i < pics.length; i++) {
			ImageView iv = new ImageView(this);
			iv.setLayoutParams(mParams);
			iv.setBackgroundResource(pics[i]);
			views.add(iv);
		}
		vp = (ViewPager) findViewById(R.id.viewPager);
		// 初始化Adapter
		vpAdapter = new ViewPagerAdapter(views);
		vp.setAdapter(vpAdapter);
		// 绑定回调
		vp.setOnPageChangeListener(this);
//		}
	}

	/**
	 * 设置当前的引导页
	 */
	private void setCurView(int position) {
		if (position < 0 || position >= pics.length) {
			return;
		}
		vp.setCurrentItem(position);
	}

	// 当滑动状态改变时调用
	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	// 当当前页面被滑动时调�?
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub

	}

//	private void initClick() {
//		registerBtn.setOnClickListener(this);
//		loginBtn.setOnClickListener(this);
//	}
//
//	private void initView() {
//		registerBtn = (Button) findViewById(R.id.regist_welcome);
//		loginBtn = (Button) findViewById(R.id.login_welcome);
//	}

	/**
	 * 寰楀埌褰撳墠杞欢鐨勭増鏈悕绉?
	 * 
	 * @return
	 */
	public String getVersion() {
		// 鍖呯鐞嗗櫒
		PackageManager pm = getPackageManager();
		// 鍔熻兘娓呭崟鏂囦欢
		try {
			PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);
			return packageInfo.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			return "";
		}
	}

	@Override
	public void onClick(View v) {
//		switch (v.getId()) {
//		case R.id.regist_welcome:
//			Intent it = new Intent(WelcomActivity.this,
//					RegisterSecondActivity.class);
//			startActivity(it);
//			WelcomActivity.this.finish();
//			break;
//
//		case R.id.login_welcome:
//			
//			Intent it1 = new Intent(WelcomActivity.this, BeginActivity.class);
//			startActivity(it1);
//			WelcomActivity.this.finish();
//			// Intent it1 = new Intent(WelcomActivity.this, MainActivity.class);
//			// startActivity(it1);
//			break;
//		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exit();
			return false;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	public void exit() {
		if (!isExit) {
			isExit = true;
			Toast.makeText(getApplicationContext(), "再按一次退出程序",
					Toast.LENGTH_SHORT).show();
			mHandler.sendEmptyMessageDelayed(0, 2000);
		} else {
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			startActivity(intent);
			System.exit(0);
		}
	}

	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			isExit = false;
		}
	};
	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}
}
